package com.myexample.hmsdemo.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.myexample.hmsdemo.R;
import com.myexample.hmsdemo.presenter.MyPresenter;

public class MainActivity extends AppCompatActivity implements IMainView {
    private MyPresenter presenter;
    private Button btn_login,btn_logout;
    private final Handler handler=new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        presenter=new MyPresenter();
        presenter.setIMainView(this);

        btn_login=findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(presenter.getHuaweiLogin(MainActivity.this),MyPresenter.LOGIN_CODE);
            }
        });

        btn_logout=findViewById(R.id.btn_logout);
        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.logout();

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==MyPresenter.LOGIN_CODE){
            presenter.setAccount(data);
            if(presenter.isLogin()){
                presenter.showShortToast("登录成功");
            }else{
                presenter.showShortToast("登录失败");
            }
        }
    }

    @Override
    public void showShortToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLongToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }

}